﻿#Region "Copyright/License"

'  Copyright (C) 2011, 2012, 2013 Paycircuit.com, Thinking Spot, Ed Chavez
'  http://goo.gl/jty8r
' 
'  Licensed under the Apache License, Version 2.0 (the "License");
'  you may not use this file except in compliance with the License.
'  You may obtain a copy of the License at
' 
'       http://www.apache.org/licenses/LICENSE-2.0
' 
'  Unless required by applicable law or agreed to in writing, software
'  distributed under the License is distributed on an "AS IS" BASIS,
'  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'  See the License for the specific language governing permissions and
'  limitations under the License.

#End Region

Imports System.Text
Imports System.Runtime.Serialization.Json
Imports System.IO

Namespace google.iap

    Public Module JSONHelpers

        ''' <summary>
        ''' Serialize to JSON string
        ''' </summary>
        ''' <param name="o">Object to serialize</param>
        ''' <returns>JSON string</returns>
        ''' <remarks></remarks>
        Function dataContractToJSON(ByVal o As Object) As String
            Dim _js As New DataContractJsonSerializer(o.GetType, New DataContractJsonSerializerSettings() With
                                                                 {
                                                                     .EmitTypeInformation = Runtime.Serialization.EmitTypeInformation.Never
                                                                 }
                                                             )
            Dim _str As String = String.Empty
            Using _ms As New MemoryStream
                Try
                    _js.WriteObject(_ms, o)
                    _ms.Position = 0
                    _str = New UTF8Encoding(True, True).GetString(_ms.ToArray)
                Catch ex As Exception
                    Throw New Exception(ex.Message)
                Finally
                    _ms.Close()
                End Try
            End Using
            Return _str
        End Function

        ''' <summary>
        ''' Deserialize to JWTHeaderObject or InAppItemObject
        ''' </summary>
        ''' <param name="jwtString">Base64url encoded string representing a JWTHeaderObject or InAppItemObject</param>
        ''' <param name="o">Object to deserialize to (e.g. JWTHeaderObject or InAppItemObject)</param>
        ''' <returns>JWTHeaderObject or InAppItemObject</returns>
        ''' <remarks>Escape processing is performed at deserialization</remarks>
        Function dataContractJSONToObj(ByVal jwtString As String, ByVal o As Object) As Object
            Dim _js As New DataContractJsonSerializer(o.GetType)
            jwtString = JWTHelpers.jwtDecodeB64Url(jwtString)
            Using _ms As New MemoryStream(New UTF8Encoding(True, True).GetBytes(jwtString))
                Try
                    o = _js.ReadObject(_ms)
                Catch ex As Exception
                    o = Nothing
                Finally
                    _ms.Close()
                End Try
            End Using
            Return o
        End Function


    End Module

End Namespace

